import Vue, { DirectiveOptions } from 'vue'
// import "tailwindcss/tailwind.css"
import 'windi.css'
import 'normalize.css'
import ElementUI from 'element-ui'
import SvgIcon from 'vue-svgicon'
import echarts from 'echarts'

import '@/styles/element-variables.scss'
import '@/styles/index.scss'
import '@/styles/common.scss'

import App from '@/App.vue'
import store from '@/store'
import { AppModule } from '@/store/modules/app'
import router from '@/router'
import i18n from '@/lang'
import '@/icons/components'
import '@/permission'
import '@/utils/error-log'
import '@/pwa/register-service-worker'
import * as directives from '@/directives'
import * as filters from '@/filters'
import { getAction } from '@/api/common'
import common from '@/utils/common'
import { OemModule } from '@/store/modules/oem'
import { useApollo, apolloProvider } from './utils/apollo'
import friendlyTime from './utils/friendlyTime'
import batchSelect from './components/BatchSelect/select'
Vue.prototype.$echarts = echarts
Vue.prototype.$batchSelect = batchSelect
Vue.use(ElementUI, {
  size: AppModule.size, // Set element-ui default size
  i18n: (key: string, value: string) => i18n.t(key, value),
})

Vue.use(SvgIcon, {
  tagName: 'svg-icon',
  defaultWidth: '1em',
  defaultHeight: '1em',
})
useApollo(Vue)
Vue.use(friendlyTime)
Vue.directive('', {})
// Register global directives
Object.keys(directives).forEach((key) => {
  Vue.directive(key, (directives as { [key: string]: DirectiveOptions })[key])
})

// Register global filter functions
Object.keys(filters).forEach((key) => {
  Vue.filter(key, (filters as { [key: string]: Function })[key])
})

Vue.config.productionTip = false
Vue.prototype.getAction = getAction
Vue.prototype.$common = common
Vue.prototype.exe = new Proxy(
  {},
  {
    get(target, p, rec) {
      return function () {
        console.log(
          `客户端调用方法[${p.toString()}], 使用VUE.$on('${p.toString()}',() => {})接收`
        )
        // eslint-disable-next-line prefer-rest-params
        console.log('参数', ...arguments)
        // eslint-disable-next-line prefer-rest-params
        VUE.$emit(p.toString(), ...arguments)
      }
    },
  }
)
OemModule.Init().then(() => {
  window.VUE = new Vue({
    router,
    store,
    i18n,
    apolloProvider,
    render: (h) => h(App),
  }).$mount('#app')
})

// 在一定程度上防止调试

if (process.env.NODE_ENV === 'production') {
  let F12Count = 0
  document.onkeydown = function () {
    // eslint-disable-next-line prefer-rest-params
    const e = window.event || arguments[0]
    if (e.keyCode === 123) {
      F12Count++
      return false
    } else if (e.ctrlKey && e.shiftKey && e.keyCode === 73) {
      return false
    } else if (e.ctrlKey && e.keyCode === 85) {
      return false
    } else if (e.ctrlKey && e.keyCode === 83) {
      return false
    }
    F12Count = 0
  }
  document.oncontextmenu = function () {
    if (F12Count >= 5) {
      VUE.$common.ExeInvoke(function (f) {
        f.ShowDebugButton()
      })
    }
    return false
  }
}
